package org.water.config;

import java.util.Map;
import javax.persistence.EntityManager;
import javax.sql.DataSource;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.water.base.support.SimpleBaseRepositoryFactoryBean;

/**
 * 默认jpa配置
 * @author qzy
 *
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef="entityManagerFactory",transactionManagerRef="defaultTransactionManager",
basePackages= { "**.repository" },repositoryFactoryBeanClass=SimpleBaseRepositoryFactoryBean.class,
repositoryImplementationPostfix="Impl")
public class DynamicJpaConfig {
    
	protected Logger log=LogManager.getLogger(getClass());
	
	@Autowired
    private JpaProperties jpaProperties;
	
	@Bean @Qualifier("jpaProperties")
    @ConfigurationProperties("spring.jpa")
    public JpaProperties jpaProperties()
    {
		return new JpaProperties();
    }

    @Autowired @Qualifier("defaultDataSource")
    private DataSource dataSource;

    @Bean(name = "defaultEntityManagerPrimary")
    @Primary
    public EntityManager entityManagerDefault(EntityManagerFactoryBuilder builder) {
    	log.info("create entityManager");
        return entityManagerFactoryDefault(builder).getObject().createEntityManager();
    }

    @Bean(name = "entityManagerFactory")
    @Primary
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryDefault (EntityManagerFactoryBuilder builder) {
    	log.info("create entityManagerFactory");
    	return builder
                .dataSource(dataSource)
                .properties(getVendorProperties(dataSource))
                .packages("**.entity") //设置实体类所在位置
                .persistenceUnit("primaryPersistenceUnit")
                .build();
    }

    private Map<String, String> getVendorProperties(DataSource dataSource) {
        return jpaProperties.getProperties();
    }

    @Bean(name = "defaultTransactionManager")
    @Primary
    PlatformTransactionManager transactionManagerDefault(EntityManagerFactoryBuilder builder) {
    	log.info("create transactionManager");
        return new JpaTransactionManager(entityManagerFactoryDefault(builder).getObject());
    }

}